﻿using System;
using System.Globalization;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Animoso.Data;
using Animoso.Domain;
using Animoso.Domain.Data;
using Animoso.Web.Binders;
using Animoso.Web.IoC;
using StructureMap;

namespace Animoso.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("favicon.ico");
            routes.IgnoreRoute("Content");
            routes.IgnoreRoute("Upload");
            routes.IgnoreRoute("css");

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );
        }

        protected void Application_Start()
        {
            SessionProvider.CreateSessionFactory();
            SetupFactory();
            ControllerBuilder.Current.SetControllerFactory(new DependencyControllerFactory());           
            RegisterRoutes(RouteTable.Routes);
            ModelBinders.Binders[typeof (Registration)] = new RegistrationBinder();
        }

        private static void SetupFactory()
        {
            ObjectFactory.Initialize(registry =>
                                         {
                                             registry.For<IRepository<Section>>().Use<BaseRepository<Section>>();
                                             registry.For<IUserRepository>().Use<UserRepository>();
                                             registry.For<IRepository<User>>().Use<UserRepository>();
                                             registry.For<IPageRepository>().Use<PageRepository>();
                                             registry.For<IRepository<Event>>().Use<BaseRepository<Event>>();
                                             registry.For<IRepository<LinkItem>>().Use<BaseRepository<LinkItem>>();
                                             registry.For<IEventRepository>().Use<EventRepository>();
                                             registry.For<ILabelRepository>().Use<LabelRepository>();
                                             registry.For<INewsItemRepository>().Use<NewsItemRepository>();
                                             registry.For<IRepository<Page>>().Use<PageRepository>();
                                             registry.For<IRepository<Administration>>().Use<BaseRepository<Administration>>();
                                             registry.For<ISponsorRepository>().Use<SponsorRepository>();
                                             registry.For<IRepository<Album>>().Use<BaseRepository<Album>>();
                                             registry.For<IRepository<Commission>>().Use<BaseRepository<Commission>>();
                                             registry.For<IRepository<Picture>>().Use<BaseRepository<Picture>>();
                                             registry.For<IRepository<Introduction>>().Use<IntroductionRepository>();
                                             registry.For<IIntroductionRepository>().Use<IntroductionRepository>();
                                             registry.For<IRepository<Registration>>().Use<BaseRepository<Registration>>();
                                             registry.For<ISectionRepository>().Use<SectionRepository>();
                                         });
        }
    }
}